package com.moxiao.yfjh.module.order.dal.mysql.brokeragerecord;

import com.moxiao.yfjh.module.order.controller.admin.brokeragerecord.vo.BlindBrokerageRecordExportReqVO;
import com.moxiao.yfjh.module.order.controller.admin.brokeragerecord.vo.BlindBrokerageRecordPageReqVO;
import com.moxiao.yfjh.module.order.controller.admin.brokeragerecord.vo.BlindBrokerageRecordRespVO;
import java.math.BigDecimal;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.framework.mybatis.core.mapper.BaseMapperX;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.order.dal.dataobject.brokeragerecord.BlindBrokerageRecordDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import com.moxiao.yfjh.module.order.controller.admin.brokeragerecord.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 相亲佣金记录 Mapper
 *
 * @author mx
 */
@Mapper
public interface BlindBrokerageRecordMapper extends BaseMapperX<BlindBrokerageRecordDO> {

    default PageResult<BlindBrokerageRecordDO> selectPage(BlindBrokerageRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BlindBrokerageRecordDO>()
                .eqIfPresent(BlindBrokerageRecordDO::getUserId, reqVO.getUserId())
                .eqIfPresent(BlindBrokerageRecordDO::getBizId, reqVO.getBizId())
                .eqIfPresent(BlindBrokerageRecordDO::getBizType, reqVO.getBizType())
                .eqIfPresent(BlindBrokerageRecordDO::getTitle, reqVO.getTitle())
                .eqIfPresent(BlindBrokerageRecordDO::getPrice, reqVO.getPrice())
                .eqIfPresent(BlindBrokerageRecordDO::getTotalPrice, reqVO.getTotalPrice())
                .eqIfPresent(BlindBrokerageRecordDO::getDescription, reqVO.getDescription())
                .eqIfPresent(BlindBrokerageRecordDO::getStatus, reqVO.getStatus())
                .eqIfPresent(BlindBrokerageRecordDO::getFrozenDays, reqVO.getFrozenDays())
                .betweenIfPresent(BlindBrokerageRecordDO::getUnfreezeTime, reqVO.getUnfreezeTime())
                .eqIfPresent(BlindBrokerageRecordDO::getSourceUserLevel, reqVO.getSourceUserLevel())
                .eqIfPresent(BlindBrokerageRecordDO::getSourceUserId, reqVO.getSourceUserId())
                .betweenIfPresent(BlindBrokerageRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BlindBrokerageRecordDO::getId));
    }

    default List<BlindBrokerageRecordDO> selectList(BlindBrokerageRecordExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<BlindBrokerageRecordDO>()
                .eqIfPresent(BlindBrokerageRecordDO::getUserId, reqVO.getUserId())
                .eqIfPresent(BlindBrokerageRecordDO::getBizId, reqVO.getBizId())
                .eqIfPresent(BlindBrokerageRecordDO::getBizType, reqVO.getBizType())
                .eqIfPresent(BlindBrokerageRecordDO::getTitle, reqVO.getTitle())
                .eqIfPresent(BlindBrokerageRecordDO::getPrice, reqVO.getPrice())
                .eqIfPresent(BlindBrokerageRecordDO::getTotalPrice, reqVO.getTotalPrice())
                .eqIfPresent(BlindBrokerageRecordDO::getDescription, reqVO.getDescription())
                .eqIfPresent(BlindBrokerageRecordDO::getStatus, reqVO.getStatus())
                .eqIfPresent(BlindBrokerageRecordDO::getFrozenDays, reqVO.getFrozenDays())
                .betweenIfPresent(BlindBrokerageRecordDO::getUnfreezeTime, reqVO.getUnfreezeTime())
                .eqIfPresent(BlindBrokerageRecordDO::getSourceUserLevel, reqVO.getSourceUserLevel())
                .eqIfPresent(BlindBrokerageRecordDO::getSourceUserId, reqVO.getSourceUserId())
                .betweenIfPresent(BlindBrokerageRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BlindBrokerageRecordDO::getId));
    }

    default PageResult<BlindBrokerageRecordRespVO> selectJoinPage(BlindBrokerageRecordPageReqVO reqVO) {
        return selectJoinPage(reqVO, BlindBrokerageRecordRespVO.class, new MPJLambdaWrapper<BlindBrokerageRecordDO>()
                .selectAll(BlindBrokerageRecordDO.class)
//
//                .leftJoin(BlindMemberUserDO.class, BlindMemberUserDO::getId, BlindBrokerageRecordDO::getUserId)
//                .selectAs(BlindMemberUserDO::getNickname, BlindBrokerageRecordRespVO::getNickName)
//                .like(StrUtil.isNotBlank(reqVO.getNickname()), BlindMemberUserDO::getNickname, reqVO.getNickname())


                .leftJoin(BlindMemberUserDO.class, BlindMemberUserDO::getId, BlindBrokerageRecordDO::getUserId, ext -> ext
                        .selectAs(BlindMemberUserDO::getNickname, BlindBrokerageRecordRespVO::getNickName)
                        .like(StrUtil.isNotBlank(reqVO.getNickname()), BlindMemberUserDO::getNickname, reqVO.getSourceUserName()))
//
                .leftJoin(BlindMemberUserDO.class, BlindMemberUserDO::getId, BlindBrokerageRecordDO::getSourceUserId, ext -> ext
                        .selectAs(BlindMemberUserDO::getNickname, BlindBrokerageRecordRespVO::getSourceUserName)
                        .or().like(StrUtil.isNotBlank(reqVO.getSourceUserName()), BlindMemberUserDO::getNickname, reqVO.getSourceUserName()))

                .like(StrUtil.isNotBlank(reqVO.getNickname()), BlindMemberUserDO::getNickname, reqVO.getNickname())
                .eq(reqVO.getUserId() != null, BlindBrokerageRecordDO::getUserId, reqVO.getUserId())
                .eq(reqVO.getBizType() != null, BlindBrokerageRecordDO::getBizType, reqVO.getBizType())
                .eq(reqVO.getStatus() != null, BlindBrokerageRecordDO::getStatus, reqVO.getStatus())
                .like(StrUtil.isNotBlank(reqVO.getTitle()), BlindBrokerageRecordDO::getTitle, reqVO.getTitle())
                .orderByDesc(BlindBrokerageRecordDO::getCreateTime));
    }

    /**
     * 已提现金额
     */
    @Select("SELECT IFNULL(sum(price),0.00) from blind_brokerage_withdraw t where user_id = #{userId} and status = 10")
    BigDecimal getWithdrawn(@Param("userId") Long userId);

    /**
     * 当前用户总佣金
     */
    @Select("SELECT IFNULL(sum(price),0.00)  from blind_brokerage_record t where user_id = #{userId}")
    BigDecimal getWithdrawable(@Param("userId") Long userId);

    /**
     * 提现中
     */
    @Select("SELECT IFNULL(sum(price),0.00) from blind_brokerage_withdraw t where user_id = #{userId} and status = 0")
    BigDecimal getWithdrawalProgress(@Param("userId") Long userId);

    @Select("SELECT count(*) from blind_member_user t where distributors = #{distributors}")
    Integer getTeam(@Param("distributors") String distributors);
}
